#include "CEffectUnit.h"
#include "IEffectUnitProp.h"
#include "CEffectUnitProp.h"
#include "ITexture.h"
#include "IResourceManager.h"

BEGIN_WARE

CEffectUnit::CEffectUnit( IEffectUnitProp* pEffectUnitProp )
: m_pEffectUnitProp(pEffectUnitProp)
, m_uStartTime(0)
, m_pResMgr(NULL)
, m_nEffectUnitType(0)
{
}

CEffectUnit::~CEffectUnit()
{
	ReleaseAllTexture();
}

VBOOL CEffectUnit::Create( IResourceManager* pResMgr )
{
	if( !pResMgr )
		return VFALSE;

	m_pResMgr = pResMgr;

	CreateTexture();

	return VTRUE;
}

IEffectUnitProp* CEffectUnit::GetEffectUnitProp()
{
	return m_pEffectUnitProp;
}

void CEffectUnit::SetEffectUnitProp( IEffectUnitProp* pProp )
{	
}

//////////////////////////////////////////////////////////////////////////
int32 CEffectUnit::GetType()
{
	return m_nEffectUnitType;
}

void CEffectUnit::CreateTexture()
{
	if( !m_pResMgr )
		return;

	ReleaseAllTexture();

	CEffectUnitProp* pProp = (CEffectUnitProp*)m_pEffectUnitProp;
	m_nEffectUnitType = pProp->GetType();

	ITexture* pTex = NULL;
	NameMap& TexMap = pProp->m_mapTextureFileName;
	NameMap::iterator iterName = TexMap.begin();
	for( ; iterName != TexMap.end(); ++iterName )
	{
		m_pResMgr->CreateTexture( iterName->second.c_str(), &pTex, WARE_FMT_DXT5, 1, VFALSE );
		if( !pTex )
			continue;

		pTex->Grab();
		m_mapTexture.insert( make_pair( iterName->first, pTex ) );
	}	
}

void CEffectUnit::ReleaseAllTexture()
{
	map< float32, ITexture* >::iterator iter = m_mapTexture.begin();
	for( ; iter != m_mapTexture.end(); ++iter )
	{
		if( iter->second )
			iter->second->Drop();
	}

	m_mapTexture.clear();
}

END_WARE